#ifndef TCP_SERVER_H
#define TCP_SERVER_H

#include "tcp_client.h"

class TcpServer : public QTcpServer
{
    Q_OBJECT
public:
    explicit TcpServer(QObject *parent = 0);
    ~TcpServer();

protected:
    void incomingConnection(qintptr handle);

private:
    QList<TcpClient *> clients;
    QList<QThread *> clientsThreads;

    /**
     * @brief handleChatHash
     *        记录TCP对应的业务类实例
     */
    QHash<qintptr, TcpClient*> m_handleTcpClientHash;
    /**
     * @brief handleThreadHash
     *        记录TCP对应的子线程
     */
    QHash<qintptr,QThread*> m_handleThreadHash;

private slots:
    void newConnection();
    // void disconnected();
    void disconnected(qintptr socketDescriptor);

signals:
    void sendData(const QString &ip, int port, const QString &data);
    void receiveData(const QString &ip, int port, const QString &data);

    void clientConnected(const QString &ip, int port);
    void clientDisconnected(const QString &ip, int port);

public slots:
    //启动服务
    bool start(QString ip, int port);
    //停止服务
    void stop();

    //指定连接发送数据
    void writeData(const QString &ip, int port, const QString &data);
    //对所有连接发送数据
    void writeData(const QString &data);

    //断开指定连接
    void remove(const QString &ip, int port);
    //断开所有连接
    void remove();
};
#endif // TCP_SERVER_H
